<?php

class users extends Module {
	
	public function __construct () {
		parent::__construct ('users');
	}
	
	public function __default () {
		throw new NotFound;
	}
	
	public function groups () {
		$oTypeUser = TypesController::getInstance()->getTypeByName ('user');
		
		if (isset ($_POST['login']) && $_POST['login'] != '' && $_POST['login'] != ExplaySession::getUser()->getValue ('login')) {
			$sel = new Selection;
			$sel->addType ($oTypeUser);
			$sel->addFilter ('login', $_POST['login']);
			
			$res = $sel->getList ();
			
			if (count ($res)) {
				$oUser = ObjectsController::getInstance()->getObject ($oTypeUser, $res[0][0]);
				
				if (getRequest ('add_admin','bool')) {
					
					if ($oUser->getValue ('group') == 0) {
						$oUser->setValue ('group', 2);
					} else {
						$oUser->setValue ('group', 0);
					}
					
					$oUser->save ();
					
					redirect (HTTP_HOST . '/admin/users/groups');
				} elseif (getRequest ('add_moderator','bool')) {
					
					if ($oUser->getValue ('group') == 1) {
						$oUser->setValue ('group', 2);
					} else {
						$oUser->setValue ('group', 1);
					}
					
					$oUser->save ();
					
					redirect (HTTP_HOST . '/admin/users/groups');
				} 
			}
		}
		
		$sel = new Selection;
		$sel->addType ($oTypeUser);
		$sel->addFilterIn ('group', array (0, 1));
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('users/groups');
		
		foreach ($aObjects as $object) {
			$object->setAttribute ('group', $object->getValue ('group') == 0 ? 'admin' : 'moderator');

			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function delete () {
		$objectsController = ObjectsController::getInstance();
		
		$response = new ModuleResponse;
		$response->setTemplate('users/delete');
		
		$oTypeDel = TypesController::getInstance()->getTypeByName('user_delete');
		
		if (getRequest('deleted','bool')) {
			$response->addObject(new SystemMessage(lang('admin_user_deledted',__CLASS__)));
		}
		
		if (getRequest('request_deleted','bool')) {
			$response->addObject(new SystemMessage(lang('admin_user_request_deledted',__CLASS__)));
		}
		
		if (isset($_POST['login'])) {
			if ($_POST['login'] == '') {
				$response->addObject(new SystemError(lang('admin_error_user_not_found',__CLASS__)));
				return $response;
			}
			
			$oTypeUser = TypesController::getInstance()->getTypeByName ('user');
			
			$sel = new Selection;
			$sel->addType($oTypeUser);
			$sel->addFilter('login', $_POST['login']);
			$sel->addLimit(1);
			
			$res = $sel->getList();
			
			if (count($res) && $res[0][0] != ExplaySession::getUserId()) {
				ObjectsController::getInstance()->getObject($oTypeUser, $res[0][0])->delete();
				
				redirect(HTTP_HOST . '/admin/users/delete?deleted');
			} else {
				$response->addObject(new SystemError(lang('admin_error_user_not_found',__CLASS__)));
			}
		}
		
		if (isset($_POST['request']) && is_array($_POST['request'])) {
			$oTypeUser = TypesController::getInstance()->getTypeByName ('user');
			
			foreach ($_POST['request'] as $requestId) {
				$oRequest = $objectsController->getObject($oTypeDel, $requestId);
				
				if ($oUser = $objectsController->getObject($oTypeUser, $oRequest->getValue('user_id'))) {
					$oUser->delete();
				}
				
				$oRequest->delete();
				
				redirect(HTTP_HOST . '/admin/users/delete?deleted');
			}
		}
		
		$sel = new Selection;
		$sel->addType($oTypeDel);
		
		$aObjects = $objectsController->getObjectsList($sel->getList());
		
		foreach ($aObjects as $object) {
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function delete_request() {
		$this->expectSecureGET();

		if (!$object = ObjectsController::getInstance()->getObject(TypesController::getInstance()->getTypeByName('user_delete'), getRequest('param0', 'int'))) {
			throw new NotFound;
		}
		
		$object->delete();
		
		redirect(HTTP_HOST . '/admin/users/delete?request_deleted');
	}
}